package calysoft;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
 *
 * @author ING. Tamo Minaya Ronald Jorge
 */
public class DB {
    private Connection conn;
    
    
    private String registar(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            return "";
        } catch (Exception e) {
            return "";
        }
    }
    
    public void conectar(){
        registar();
        obtenerConeccion(); 
    }
    
    public int ejecutarUpdate (String query){
        try {
            Statement st = this.crearSentencia();
            return st.executeUpdate(query);
        } catch (SQLException ex) {
            return -1;
        }
    }
    public ResultSet ejecutarQuery (String query){
        try {
            Statement st = this.crearSentencia();
            return st.executeQuery(query);
        } catch (SQLException ex) {
            return null;
        }
    }
    
    private String obtenerConeccion(){
        try {
            String url= "jdbc:mysql://172.16.0.100:6021/";
            String dbName = "xxxx";
            String userName = "xxxx";
            String password = "xxxx";
            this.conn = DriverManager.getConnection(url+dbName,userName,password);
            //JOptionPane.showMessageDialog(null,"obtener 6021");
            return "conexcion ok";
        } catch (Exception e) {
            //JOptionPane.showMessageDialog(null,"fallisa");
            return "coneccion fallida";
        }
              
     }
    
    private Statement crearSentencia (){
        try {
            return this.conn.createStatement();
        } catch (Exception e) {
            return null;
        }
        
    }
    
}
